﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PowerControl
{
    public partial class MobileInfo : Form
    {
        private MobileDevice mobile;
        private Simulation simu;

        public MobileInfo(Simulation sim)
        {
            InitializeComponent();
            this.simu = sim;
            this.mobile = sim.MobileDevices[0];
        }

        public Simulation AttachedSimulation
        {
            get
            {
                return this.simu;
            }
            set
            {
                this.simu = value;
            }
        }

        private void UpdateMobileInformation(MobileDevice m)
        {
            this.textBox4.Text = m.ID.ToString();
            this.textBox1.Text = m.Position.X.ToString();
            this.textBox2.Text = m.Position.Y.ToString();
            this.textBox3.Text = m.Position.Z.ToString();
        }

        private void MobileInfo_Load(object sender, EventArgs e)
        {
            timer1.Interval = 10;
            timer1.Enabled = true;
            for (int i = 0; i < simu.MobileDevices.Count; ++i)
            {
                MobileDevice m = simu.MobileDevices[i];
                listBox1.Items.Add(m.ID);
            }
            this.mobile = simu.MobileDevices[0];

            this.UpdateMobileInformation(this.mobile);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //Update the displayed information on the scope
            if (this.mobile != null)
            {
                //SIR
                scope1.Channels[0].Data.AddYPoint(Utils.wattTodB(this.mobile.UplinkEmittedPower / this.mobile.UplinkInterference));
            
                //Transmitting Power
                scope2.Channels[0].Data.AddYPoint(Utils.wattTodB(this.mobile.Antenna.TransmittingPower * 1000.0));
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != "" && listBox1.SelectedIndex != -1)
            {
                this.scope1.Channels[0].Data.Clear();
                this.mobile = this.simu.MobileDevices[listBox1.SelectedIndex];
                this.UpdateMobileInformation(mobile);
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                mobile.Position.X = Double.Parse(textBox1.Text);
            }
        }
    }
}
